set _j_function=%1

call :pack_args _j_args 1 %*
call :!_j_function! !_j_args!

call :unset _j_function _j_args
exit /B

:init
	set j_os_name=win
	set j_cmake=!j_jenny_root_dir!\..\tools\cmake\3.7.1\bin\cmake.exe
	
	exit /B

:extract_git_url
	for /F "usebackq tokens=1,2* delims=	 " %%i in (`git remote -v`) do (
		set %1=%%j
	)
	exit /B

:sync_cmake
	set cmake_dir=..\tools\cmake\3.7.1
	if not exist !cmake_dir! ( 
		md !cmake_dir! || ( echo "Failed to create dir to download cmake" 
		 exit /B 1
		)
	)
	pushd !cmake_dir!
	if exist .git (
		cmd /E:ON /V:ON /C git pull
	) else (
		cmd /E:ON /V:ON /C git clone git@gitee.com:wingfire/cmake-win-3.7.1.git . 
	)
	popd
	
	exit /B

:: param: start_path detect_path result_path variable
:detect_path
	set _j_arg=%~1
	set _j_disk=%~d1\


	:_loop_detect_path
		if exist "!_j_arg!%2" (
			set %4=!_j_arg!%3
			call :unset _j_arg _j_disk 
			exit /B
		)

		for %%a in ( "!_j_arg!." ) do set _j_arg=%%~dpa
		if "!_j_arg!" == "!_j_disk!" (
			call :unset _j_arg _j_disk %4
			exit /B 1
		)

	goto :_loop_detect_path

:detect_tools_path
	call :detect_path %1 tools\cmake tools J_TOOLS_PATH
	exit /B

:detect_external_path
	call :detect_path %1 external external J_EXTERNAL_PATH
	exit /B


:detect_cmake_path 
	call :detect_tools_path %1
	if defined J_CMAKE_VERSION (
		set J_CMAKE_PATH=!J_TOOLS_PATH!\cmake\!J_CMAKE_VERSION!\bin\cmake.exe
		if exist "!J_CMAKE_PATH!" exit /B 0
		exit /B 1
	)

	set J_CMAKE_PATH=
	for /F "usebackq delims=*"  %%a in (`where /F cmake.exe`) do set J_CMAKE_PATH=%%~a

	exit /B

:do_nothing
	exit /B


:print
	set _j_echo="!J_CMAKE_PATH!" -E cmake_echo_color --no-newline

	if /I "%1"=="ERROR" (
		set _j_msg_type="[ERROR] "
		set _j_echo=!_j_echo! --red --bold
	) else if /I "%1"=="INFO" (
		set _j_msg_type="[INFO] "
		set _j_echo=!_j_echo! --green
	) else if /I "%1"=="DEBUG" (
		set _j_msg_type="[DEBUG] "
		set _j_echo=!_j_echo! --white
	) else (
		set _j_msg_type=++ 
		set _j_echo=!_j_echo! --normal
	)


	call :pack_args _j_print_args 1 %*
	set _j_msg_text=""
	if not defined J_CMAKE_PATH (
		for %%a in (!_j_msg_type!) do echo %%~a !_j_print_args!
	)else (
		for %%a in (!_j_msg_type!) do (
			set _j_msg_text=%%~a
			for %%b in (!_j_print_args!) do (
				set _j_msg_text=!_j_msg_text! " " %%b
			)
			!_j_echo! !_j_msg_text! 
		)
		echo.
	)

	call :unset _j_msg_type _j_print_args _j_echo _j_msg_text
	exit /B

:set_echo
	set j_log_info=call !j_script_dir!tools\function.bat print INFO 
	set j_log_error=call !j_script_dir!tools\function.bat print ERROR 
	set j_log_mesg=call !j_script_dir!tools\function.bat print  MESG 
	set j_log_debug=call !j_script_dir!tools\function.bat print DEBUG 

	exit /B

:pack_args
	set _j_ret=%1
	set /A _j_skip=%2 + 2

	:_loop_pack_args_skip
	if !_j_skip! GTR 0 (
		shift
		set /A _j_skip=!_j_skip! - 1
		goto _loop_pack_args_skip
	)

	set _j_tmp=%1
	:_loop_pack_args
	shift
	if "%~1" NEQ "" ( 
		set _j_tmp=!_j_tmp! %1
		goto _loop_pack_args
	)

	set !_j_ret!=!_j_tmp!

	call :unset _j_tmp _j_ret
	exit /B

:unset
	:_loop_unset
	if "%1" NEQ "" ( 
		set %1=
		shift
		goto _loop_unset
	)
	
	exit /B



:parse_args
	set j_config=Debug
	set j_architecture=x64
	set j_toolset=vc
	set j_toolset_ver=14
	set j_generator=Visual Studio 14
	set j_vsvariable="!VS140COMNTOOLS!vsvars32.bat"
	set j_platform=win
	set j_target=
:parse_args_start
	if /I "%1"=="-c" (
		if /I "%2"=="debug" (
			set j_config=Debug
		) else if /I "%2"=="release" (
			set j_config=Release
		) else (
			echo bad config: "%2"
			goto :parse_args_end
		)
	) else if /I "%1"=="-a" (
		if /I "%2"=="x64" (
			set j_architecture=x64
		) else if /I "%2"=="x86" (
			set j_architecture=x86
		) else (
			echo bad architecture: "%2"
			goto :parse_args_end
		)
	) else if /I "%1"=="-p" (
		if /I "%2"=="win" (
			set j_platform=win
		) else (
			echo bad platform: "%2"
			goto :parse_args_end
		)
	) else if /I "%1"=="-t" (
		if /I "%2"=="vc11" (
			set j_toolset_ver=11			
			set j_generator=Visual Studio 11
			set j_vsvariable="!VS110COMNTOOLS!vsvars32.bat"
		) else if /I "%2"=="vc12" (
			set j_toolset_ver=12
			set j_generator=Visual Studio 12
			set j_vsvariable="!VS120COMNTOOLS!vsvars32.bat"
		) else if /I "%2"=="vc14" (
			set j_toolset_ver=14
			set j_generator=Visual Studio 14
			set j_vsvariable="!VS140COMNTOOLS!vsvars32.bat"
		) else (
			echo bad toolset: "%2"
			goto :parse_args_end
		)		
		if not exist !j_vsvariable! (
			echo "Failed to set toolset !j_toolset!, not found !j_vsvariable!"
			exit /B 1
		)
	) else if /I "%1"=="-d" (
		set j_target=%2

	) else if /I "%1"== "" (
			goto :parse_args_post
	) else (
			set local_arg=%1
			set first_char=!local_arg:~0,1!
			if "!first_char!" == "-" (
				echo unknown flag "%1"
				goto :parse_args_end
			) else (
				set j_config_path=%1
				goto :parse_args_post
			)
			
		 )
	shift 
	shift 
	goto :parse_args_start
	
:parse_args_post
	echo(!j_config_path:^"=!|findstr /i "^[A-Z]:[\\] ^[\\][\\]" >nul || set "j_config_path=!j_current_dir!\!j_config_path!"
	call :detect_path !j_config_path!\ jenny.cmake jenny.cmake j_config_path
	if not exist "!j_config_path!" (
		echo can't found jenny.cmake
		goto :parse_args_end
	)
	
	if "!j_architecture!" == "x64" set j_generator=!j_generator! Win64
	exit /B 0
:parse_args_end
	exit /B 1
